package com.whmnx.base.wxapi;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.whmnx.base.R;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.constants.EventConstants;

import me.goldze.mvvmhabit.bus.RxBus;


/**
 * @author Administrator
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
	public static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
		api = WXAPIFactory.createWXAPI(this, Constants.WECHAT_ID, false);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {

	}

	@SuppressLint("LongLogTag")
	@Override
	public void onResp(BaseResp resp) {
	Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);

		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			int code=resp.errCode;
			if(code==0){



				RxBus.getDefault().post(EventConstants.WEIXIN);

                finish();

			}else if(code==-1){
				Toast.makeText(this, "支付失败", Toast.LENGTH_SHORT).show();
			}else if(code==-2){
				Toast.makeText(this, "取消支付", Toast.LENGTH_SHORT).show();
				finish();
			}
		}
	}
}